import Mock from 'mockjs'
const {REACT_APP_BASE_URL,REACT_APP_BASE} =process.env;
const url = REACT_APP_BASE_URL+REACT_APP_BASE
// 设置延迟时间
Mock.setup({
    timeout: '200-400'
});
if (process.env.NODE_ENV === 'development') {
    Mock.mock(url+'/login','post', function (val) {
        const {userName, password} = JSON.parse(val.body);
        if (userName ==='admin' && password ==='123456') {
            return {
                code: 200,
                msg: '登录成功',
                data: {
                    userId: 1000,
                    userName: 'admin',
                    password: '123456',
                    token: "dnwihweh0w0183971030183971030",
                },
            }
        }else {
            return {
                code: 400,
                msg: '用户名或密码错误！',
                data: null,
            }
        }
    })
    // 查询分类接口
    Mock.mock(RegExp(url + '/queryType'), 'get', () => ({
        code: 200,
        msg: '成功',
        data: {
            records: Array.from({ length: 10 }, (_, index) => ({ // 生成10条随机数据
                id: `${index+1}`, // 自增ID
                name: `模块分类${index + 1}`,
                status: 1,
                children: Mock.Random.boolean() ? [] : null, // 50%概率有子节点
                disabled: Mock.Random.boolean(0.2), // 20%概率禁用
                createTime: Mock.Random.datetime() // 随机生成创建时间
            })),
            total: 10,
            size: 30,
            current: 1
        }
    }));
    // 查询列表接口
    Mock.mock(RegExp(url + '/queryList'), 'get', function (val) {
       const {url} = val;
       //把链接参数转成json
        const params = url.split('?')[1];
        if (params && params.length > 1) {
            const paramsArr = params.split('&');
            const paramsObj = {};
            paramsArr.forEach(item => {
                const [key, value] = item.split('=');
                paramsObj[key] = value;
            });
            const {id} = paramsObj;
            let randomNum = Math.floor(Math.random() * (20 + 1));
            return {
                code: 200,
                msg: '成功',
                data: {
                    records: Array.from({ length: randomNum }, (_, index) => ({
                        id: `${index+1}`,
                        name: id+`--模块分类子项${index + 1}`,
                        itemList: ['菜单', '按钮', '接口', '权限' ],
                        sort: 1,
                        type: "1",
                    }))
                }
            }
        }
        return {
            code: 200,
            msg: '成功',
            data: []
        }
    })
}

